package com.stylefeng.guns.modular.bigSrceen.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.area.dao.BdapAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropHmMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropQmMapper;
import com.stylefeng.guns.modular.area.dao.BdapCellPopPropHmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaPopPropQmMapper;
import com.stylefeng.guns.modular.area.model.BdapArea;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropHm;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropQm;
import com.stylefeng.guns.modular.area.model.BdapBigScreenCellPopHm;
import com.stylefeng.guns.modular.area.model.BdapSubArea;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropHm;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropQm;
import com.stylefeng.guns.modular.bigSrceen.dao.BigSrceenDao;
import com.stylefeng.guns.modular.bigSrceen.dao.SubareaPopHmDao;
import com.stylefeng.guns.modular.bigSrceen.model.MapData;
import com.stylefeng.guns.modular.map.dao.TraceDayMapper;
import com.stylefeng.guns.modular.map.model.MyTimeType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 区域大屏控制器
 *
 * @author fengshuonan
 * @Date 2018-02-28 10:09:23
 */
@Controller
@RequestMapping("/bigSrceen")
public class BigSrceenController extends BaseController {

    private String PREFIX = "/bigSrceen/bigSrceen/";
    
    @Autowired
    BdapAreaMapper areaMapper;
    @Autowired
    BdapCellPopPropHmMapper cellpopHmMapper;
    @Autowired
    BdapSubAreaMapper subareaMapper;
    @Autowired
    BigSrceenDao bigScreenMapper;
    @Autowired
    private TraceDayMapper traceDayMapper;
    @Autowired
    private SubareaPopHmDao subareaPopHmMapper;
    @Autowired
    private BdapAreaPopPropHmMapper bdapAreaPopPropHmMapper;
    @Autowired
    private BdapAreaPopPropQmMapper bdapAreaPopPropQmMapper;
    @Autowired
    private BdapSubareaPopPropQmMapper bdapSubareaPopPropQmMapper;
    

    /**
     * 跳转到区域大屏首页
     */
    @RequestMapping("")
    public String index(Model model,String areaId,String subAreaId,String areaName) {
    	model.addAttribute("areaId",areaId);
    	model.addAttribute("subAreaId",subAreaId);
    	model.addAttribute("areaName",areaName);
        return PREFIX + "bigSrceen.html";
    }
    
    /**
     * 查找区域id
     * @Description: TODO
     * @param @param beginTime
     * @param @param upTime
     * @param @return   
     * @return Object  
     * @throws
     * @author polly
     * @date 2018-3-1 09:44:09
     */
    @RequestMapping(value = "/findName")
    @ResponseBody
    public Object findAreaID(String areaId,String subAreaId) {
    	JSONArray jsonArray = new JSONArray();
    	try {
			if (ToolUtil.isEmpty(areaId)) {
				return null;
			}
			BdapArea area = new BdapArea();
			List<BdapSubArea> subAreaList = new ArrayList<BdapSubArea>();
			Wrapper<BdapSubArea> subAreaWrapper = new EntityWrapper<BdapSubArea>();
			area = areaMapper.selectById(Long.parseLong(areaId));
			if (ToolUtil.isEmpty(subAreaId)) {
				subAreaWrapper.eq("pid", areaId);
				subAreaList = subareaMapper.selectList(subAreaWrapper);
			} else {
				subAreaWrapper.eq("id", subAreaId);
				subAreaList = subareaMapper.selectList(subAreaWrapper);
			}
			List<Object> times=subareaPopHmMapper.selectObjs(new EntityWrapper<BdapSubareaPopPropHm>().setSqlSelect("max(STAT_HOUR)"));
			String maxTime=times.get(0).toString();
			if (ToolUtil.isNotEmpty(subAreaList)) {
				//新增：这里进行lac,ci的拆分循环查询每个基站下面的人口数，进行统计汇总 @polly
				for (int i = 0; i < subAreaList.size(); i++) {
					BdapSubArea bsa = subAreaList.get(i);
					List<BdapBigScreenCellPopHm> cellDatas = bigScreenMapper.selectCellDataHmList(areaId,bsa.getId().toString(),maxTime);
					//BdapBigScreenCellPopHm bdapBigScreenCellPopHm = bigScreenMapper.selectCellDataHmList(areaId,bsa.getId().toString(),maxTime).get(0);
					//Map<String,String> map = new HashMap<String,String>();
					for (int j = 0; j < cellDatas.size(); j++) {
						BdapBigScreenCellPopHm bdapBigScreenCellPopHm = cellDatas.get(j);
						if (ToolUtil.isNotEmpty(area)) {
							JSONObject jbObject = new JSONObject();
							jbObject.put("cityName", area.getCity());
							jbObject.put("lonCenter", area.getLon());
							jbObject.put("latCenter", area.getLat());
							jbObject.put("popCount", bdapBigScreenCellPopHm.getTotalUser());
							jbObject.put("lon", bdapBigScreenCellPopHm.getLon());
							jbObject.put("lat", bdapBigScreenCellPopHm.getLat());
							int hour = Integer.parseInt(bdapBigScreenCellPopHm.getStatHour().substring(8, 10))+1;
							jbObject.put("hour", hour);
							jsonArray.put(jbObject);
						} else {
							return null;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
    } 

    /**
     * 跳转到添加区域大屏
     */
    @RequestMapping("/bigSrceen_add")
    public String bigSrceenAdd() {
        return PREFIX + "bigSrceen_add.html";
    }

    /**
     * 跳转到修改区域大屏
     */
    @RequestMapping("/bigSrceen_update/{bigSrceenId}")
    public String bigSrceenUpdate(@PathVariable Integer bigSrceenId, Model model) {
        return PREFIX + "bigSrceen_edit.html";
    }

    /**
     * 获取区域大屏列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return null;
    }

    /**
     * 新增区域大屏
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除区域大屏
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改区域大屏
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 区域大屏详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }

    /**
     * 性别分布
     */
    @RequestMapping(value = "/sex")
    @ResponseBody
    public Object getSex(String areaId,String subAreaId,String maxHour,String maxsubHour){
        JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        //String maxTime=maxHour;
        //根据最新日期查找该区域下性别分布
        List<String> listsex = new ArrayList<String>();
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropHm> times=subareaPopHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxsubHour);
        	List<BdapSubareaPopPropHm> sex = subareaPopHmMapper.selectSexByTime(mytimeType);
        	for (int i = 0; i < sex.size(); i++) {
        		listsex.add(sex.get(i).getSexUsers());
			}
        }else{
        	//List<BdapAreaPopPropHm> times=bdapAreaPopPropHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxHour);
        	List<BdapAreaPopPropHm> sex = bdapAreaPopPropHmMapper.selectSexByTime(mytimeType);
        	for (int i = 0; i < sex.size(); i++) {
        		listsex.add(sex.get(i).getSexUsers());
			}
        }
        List<Map<String,Integer>> sexNumMapList = new ArrayList<Map<String,Integer>>();//存放每个基站下的性别分布
        List<String> sexTypeList = new ArrayList<String>();
        for (int i = 0; i < listsex.size(); i++) {
        	String[] sexType = listsex.get(i).split(";");
        	Map<String,Integer> map = new HashMap<String,Integer>();
        	for(String sex:sexType){
        		String[] sexInfo = sex.split(":");
        		map.put(sexInfo[0],Integer.parseInt(sexInfo[1].trim()));
        		sexTypeList.add(sexInfo[0]);
        	}
        	sexNumMapList.add(map);
		}
        //去重
        sexTypeList= new ArrayList<String>(new HashSet(sexTypeList));
        for(String key:sexTypeList){
            Integer num=0;
            JSONObject jbObject = new JSONObject();
            for(Map<String,Integer> map:sexNumMapList){
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if(key.equals(entry.getKey())){
                        num += entry.getValue();
                    }
                }
            }
            try {
				jbObject.put("name",key);
				jbObject.put("value",num);
				jsonArray.put(jbObject);
			} catch (JSONException e) {
				e.printStackTrace();
			}
        }
        return jsonArray.toString();
    }


    /**
     * 省外用户分布
     */
    @RequestMapping(value = "/outsideUser")
    @ResponseBody
    public Object getOutsideUser(String areaId,String subAreaId,String maxHour,String maxsubHour){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        //String maxTime=maxHour;
        //根据最新日期查找该区域下性别分布
        List<String> listOutsideUsers = new ArrayList<String>();
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropHm> times=subareaPopHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxsubHour);
        	List<BdapSubareaPopPropHm> sex = subareaPopHmMapper.selectOutsideUsersByTime(mytimeType);
        	for (int i = 0; i < sex.size(); i++) {
        		listOutsideUsers.add(sex.get(i).getSwCityUsers());
			}
        }else{
        	//List<BdapAreaPopPropHm> times=bdapAreaPopPropHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxHour);
        	List<BdapAreaPopPropHm> sex = bdapAreaPopPropHmMapper.selectOutsideUsersByTime(mytimeType);
        	for (int i = 0; i < sex.size(); i++) {
        		listOutsideUsers.add(sex.get(i).getProvinceUsers());
			}
        }
        List<Map<String,Integer>> outsideUserMapList = new ArrayList<Map<String,Integer>>();//存放每个基站下的性别分布
        List<String> outsideList = new ArrayList<String>();
        for (int i = 0; i < listOutsideUsers.size(); i++) {
        	String[] outside = listOutsideUsers.get(i).split(";");
        	Map<String,Integer> map = new HashMap<String,Integer>();
        	for(String os:outside){
        		if(os.contains(":")){
        			String[] osInfo = os.trim().split(":");
        			map.put(osInfo[0],Integer.parseInt(osInfo[1].trim()));
        			outsideList.add(osInfo[0]);
        		}
        	}
        	outsideUserMapList.add(map);
		}
        //去重
        List<MapData> list = new ArrayList<MapData>();
        outsideList= new ArrayList<String>(new HashSet(outsideList));
        for(String key:outsideList){
            Integer num=0;
//            JSONObject jbObject = new JSONObject();
            for(Map<String,Integer> map:outsideUserMapList){
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if(key.equals(entry.getKey())){
                        num += entry.getValue();
                    }
                }
            }
            MapData map = new MapData();
			map.setName(key);
			map.setValue(num);
			list.add(map);
//				jbObject.put("name",key);
//				jbObject.put("value",num);
//				jsonArray.put(jbObject);
        }
        //这里由于外省用户覆盖太密集，才用倒序top10的方法来完成
        Collections.sort(list, Collections.reverseOrder());
        int temp=0;
        if(list.size()>10){
        	temp = 10;
        }else{
        	temp = list.size();
        }
        try {
			for (int i = 0; i < temp; i++) {
				MapData map = list.get(i);
				JSONObject jbObject = new JSONObject();
				jbObject.put("name",map.getName());
				jbObject.put("value",map.getValue());
				jsonArray.put(jbObject);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
        return jsonArray.toString();
    }
    
    /**
     * 历史人数统计
     */
    @RequestMapping(value = "/historyNumLine")
    @ResponseBody
    public Object getHistoryNumLine(String areaId,String subAreaId,String maxDay,String maxsubDay){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropHm> times=subareaPopHmMapper.selectMaxDayTime(mytimeType);
        	//maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxsubDay);
        	List<BdapSubareaPopPropHm> historyNumLine = subareaPopHmMapper.selectHistoryNumLineByTime(mytimeType);
        	for (int i = 0; i < historyNumLine.size(); i++) {
        		BdapSubareaPopPropHm bsppHm = historyNumLine.get(i);
        		JSONObject jbObject = new JSONObject();
        		String statDate = bsppHm.getStatDate();
        		String totalUsers = bsppHm.getTotalUsers();
        		try {
					jbObject.put("date",statDate);
					jbObject.put("value",totalUsers);
					jsonArray.put(jbObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
        }else{
        	//List<BdapAreaPopPropHm> times=bdapAreaPopPropHmMapper.selectMaxDayTime(mytimeType);
        	//maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxDay);
        	List<BdapAreaPopPropHm> historyNumLine = bdapAreaPopPropHmMapper.selectHistoryNumLineByTime(mytimeType);
        	for (int i = 0; i < historyNumLine.size(); i++) {
        		BdapAreaPopPropHm bappHm = historyNumLine.get(i);
        		JSONObject jbObject = new JSONObject();
        		String statDate = bappHm.getStatDate();
        		String totalUsers = bappHm.getTotalUsers();
        		try {
					jbObject.put("date",statDate);
					jbObject.put("value",totalUsers);
					jsonArray.put(jbObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
        }
        return jsonArray.toString();
    }
    
    /**
     * 实时用户数趋势统计
     */
    @RequestMapping(value = "/realTimeNumLine")
    @ResponseBody
    public Object getRealTimeNumLine(String areaId,String subAreaId,String maxMinutes,String maxsubMinutes){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        //String maxTime="";
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropQm> times=bdapSubareaPopPropQmMapper.selectMaxMinutesTime(mytimeType);
        	//maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxsubMinutes);
        	List<BdapSubareaPopPropQm> realTimeNumLine = bdapSubareaPopPropQmMapper.selectRealTimeNumLineByTime(mytimeType);
        	for (int i = 0; i < realTimeNumLine.size(); i++) {
        		BdapSubareaPopPropQm bsppQm = realTimeNumLine.get(i);
        		JSONObject jbObject = new JSONObject();
        		String statDate = bsppQm.getStatMinutes();
        		String totalUsers = bsppQm.getTotalUsers();
        		try {
					jbObject.put("date",statDate);
					jbObject.put("value",totalUsers);
					jsonArray.put(jbObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
        }else{
        	//List<BdapAreaPopPropQm> times=bdapAreaPopPropQmMapper.selectMaxDayTime(mytimeType);
        	//maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxMinutes);
        	List<BdapAreaPopPropQm> realTimeNumLine = bdapAreaPopPropQmMapper.selectRealTimeNumLineByTime(mytimeType);
        	for (int i = 0; i < realTimeNumLine.size(); i++) {
        		BdapAreaPopPropQm bappHm = realTimeNumLine.get(i);
        		JSONObject jbObject = new JSONObject();
        		String statDate = bappHm.getStatMinutes();
        		String totalUsers = bappHm.getTotalUsers();
        		try {
					jbObject.put("date",statDate);
					jbObject.put("value",totalUsers);
					jsonArray.put(jbObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
        }
        return jsonArray.toString();
    }
    
    /**
     * 人口分布统计
     */
    @RequestMapping(value = "/populationDis")
    @ResponseBody
    public Object getPopulationDis(String areaId,String subAreaId,String maxHour,String maxsubHour){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        try {
			//根据最新日期查找该区域下性别分布
			if (subAreaId != null && !"".equals(subAreaId)) {
				mytimeType.setBeginTime(maxsubHour);
				List<BdapSubareaPopPropHm> populationDis = subareaPopHmMapper
						.selectPopulationDisByTime(mytimeType);
				for (int i = 0; i < populationDis.size(); i++) {
					BdapSubareaPopPropHm bsppHm = populationDis.get(i);
					String stayLocalUsers = bsppHm.getStayLocalUsers().trim();
					String workLocalUsers = bsppHm.getWorkLocalUsers().trim();
					JSONObject stayjbObject = new JSONObject();
					stayjbObject.put("name", "常驻人口");
					stayjbObject.put("value", Integer.parseInt(stayLocalUsers));
					jsonArray.put(stayjbObject);
					JSONObject workjbObject = new JSONObject();
					workjbObject.put("name", "工作人口");
					workjbObject.put("value", Integer.parseInt(workLocalUsers));
					jsonArray.put(workjbObject);
				}
			} else {
				mytimeType.setBeginTime(maxHour);
				List<BdapAreaPopPropHm> populationDis = bdapAreaPopPropHmMapper
						.selectPopulationDisByTime(mytimeType);
				for (int i = 0; i < populationDis.size(); i++) {
					BdapAreaPopPropHm bsppHm = populationDis.get(i);
					String stayLocalUsers = bsppHm.getStayLocalUsers().trim();
					String workLocalUsers = bsppHm.getWorkLocalUsers().trim();
					JSONObject stayjbObject = new JSONObject();
					stayjbObject.put("name", "常驻人口");
					stayjbObject.put("value", Integer.parseInt(stayLocalUsers));
					jsonArray.put(stayjbObject);
					JSONObject workjbObject = new JSONObject();
					workjbObject.put("name", "工作人口");
					workjbObject.put("value", Integer.parseInt(workLocalUsers));
					jsonArray.put(workjbObject);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
    }
    
    /**
     * 用户年龄段分布
     */
    @RequestMapping(value = "/agePie")
    @ResponseBody
    public Object getAgePie(String areaId,String subAreaId,String maxHour,String maxsubHour){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        String maxTime="";
        //根据ID查找小时表中有数据的最新日期
        List<String> listagePie = new ArrayList<String>();
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropHm> times=subareaPopHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxsubHour);
        	List<BdapSubareaPopPropHm> agePieData = subareaPopHmMapper.selectAgePieByTime(mytimeType);
        	for (int i = 0; i < agePieData.size(); i++) {
        		listagePie.add(agePieData.get(i).getAgeUsers());
			}
        }else{
        	List<BdapAreaPopPropHm> times=bdapAreaPopPropHmMapper.selectMaxHourTime(mytimeType);
        	maxTime=times.get(0).getStatDate();
        	mytimeType.setBeginTime(maxTime);
        	List<BdapAreaPopPropHm> agePieData = bdapAreaPopPropHmMapper.selectAgePieByTime(mytimeType);
        	for (int i = 0; i < agePieData.size(); i++) {
        		listagePie.add(agePieData.get(i).getAgeUsers());
			}
        }
        List<Map<String,Integer>> agePieMapList = new ArrayList<Map<String,Integer>>();//存放每个基站下的性别分布
        List<String> agePieList = new ArrayList<String>();
        for (int i = 0; i < listagePie.size(); i++) {
			String agePie = listagePie.get(i);
			String[] agePies = agePie.split(";");
			Map<String,Integer> map = new HashMap<String,Integer>();
			for (int j = 0; j < agePies.length; j++) {
				String[] ageInfo = agePies[j].split(":");
				map.put(ageInfo[0],Integer.parseInt(ageInfo[1].trim()));
				agePieList.add(ageInfo[0]);
			}
			agePieMapList.add(map);
		}
        //去重
        agePieList= new ArrayList<String>(new HashSet(agePieList));
        for(String key:agePieList){
            Integer num=0;
            JSONObject jbObject = new JSONObject();
            for(Map<String,Integer> map:agePieMapList){
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if(key.equals(entry.getKey())){
                        num += entry.getValue();
                    }
                }
            }
            try {
				jbObject.put("name",key);
				jbObject.put("value",num);
				jsonArray.put(jbObject);
			} catch (JSONException e) {
				e.printStackTrace();
			}
        }
        return jsonArray.toString();
    }
    
    /**
     * 省内用户数统计
     */
    @RequestMapping(value = "/innerNums")
    @ResponseBody
    public Object getInnerNums(String areaId,String subAreaId,String maxHour,String maxsubHour){
    	JSONArray jsonArray = new JSONArray();
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        //String maxTime="";
        //根据ID查找小时表中有数据的最新日期
        List<String> listinnerNums = new ArrayList<String>();
        if(subAreaId!=null && !"".equals(subAreaId)){
        	//List<BdapSubareaPopPropHm> times=subareaPopHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxsubHour);
        	List<BdapSubareaPopPropHm> innerNums = subareaPopHmMapper.selectInnerNumsTime(mytimeType);
        	for (int i = 0; i < innerNums.size(); i++) {
        		listinnerNums.add(innerNums.get(i).getSnCityUsers());
			}
        }else{
        	//List<BdapAreaPopPropHm> times=bdapAreaPopPropHmMapper.selectMaxHourTime(mytimeType);
        	//maxTime=times.get(0).getStatHour();
        	mytimeType.setBeginTime(maxHour);
        	List<BdapAreaPopPropHm> innerNums = bdapAreaPopPropHmMapper.selectInnerNumsTime(mytimeType);
        	for (int i = 0; i < innerNums.size(); i++) {
        		listinnerNums.add(innerNums.get(i).getSnCityUsers());
			}
        }
        List<Map<String,Integer>> innerNumsMapList = new ArrayList<Map<String,Integer>>();//存放每个基站下的性别分布
        List<String> innerNumsList = new ArrayList<String>();
        for (int i = 0; i < listinnerNums.size(); i++) {
			String agePie = listinnerNums.get(i);
			String[] agePies = agePie.split(";");
			Map<String,Integer> map = new HashMap<String,Integer>();
			for (int j = 0; j < agePies.length; j++) {
				String[] ageInfo = agePies[j].split(":");
				map.put(ageInfo[0],Integer.parseInt(ageInfo[1].trim()));
				innerNumsList.add(ageInfo[0]);
			}
			innerNumsMapList.add(map);
		}
        //去重
        innerNumsList= new ArrayList<String>(new HashSet(innerNumsList));
        for(String key:innerNumsList){
            Integer num=0;
            JSONObject jbObject = new JSONObject();
            for(Map<String,Integer> map:innerNumsMapList){
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if(key.equals(entry.getKey())){
                        num += entry.getValue();
                    }
                }
            }
            try {
				jbObject.put("name",key);
				jbObject.put("value",num);
				jsonArray.put(jbObject);
			} catch (JSONException e) {
				e.printStackTrace();
			}
        }
        return jsonArray.toString();
    }
    
    /**
     * 获取最大时间
     */
    @RequestMapping(value = "/maxTime")
    @ResponseBody
    public Object getMaxTime(String areaId,String subAreaId){
        if (ToolUtil.isEmpty(areaId)) {
            return null;
        }
        //设置查询条件
        MyTimeType mytimeType = new MyTimeType();
        mytimeType.setAreaId(areaId);
        mytimeType.setSubAreaId(subAreaId);
        List<BdapAreaPopPropHm> hour=bdapAreaPopPropHmMapper.selectMaxHourTime(mytimeType);
        List<BdapAreaPopPropHm> day=bdapAreaPopPropHmMapper.selectMaxDayTime(mytimeType);
        List<BdapAreaPopPropQm> minutes=bdapAreaPopPropQmMapper.selectMaxDayTime(mytimeType);
        List<BdapSubareaPopPropHm> subhour=subareaPopHmMapper.selectMaxHourTime(mytimeType);
        List<BdapSubareaPopPropHm> subday=subareaPopHmMapper.selectMaxDayTime(mytimeType);
        List<BdapSubareaPopPropQm> subminutes=bdapSubareaPopPropQmMapper.selectMaxMinutesTime(mytimeType);
    	String submaxHourTime=subhour.get(0).getStatHour();
    	String submaxDayTime=subday.get(0).getStatDate();
    	String subminutesTime = subminutes.get(0).getStatMinutes();
    	String hourTime = hour.get(0).getStatHour();
    	String dayTime = day.get(0).getStatDate();
    	String minutesTime = minutes.get(0).getStatMinutes();
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHH");
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
    	JSONObject jbObject = new JSONObject();
    	try {
    		Date date = sdf.parse(submaxHourTime);
    		String dateString = formatter.format(date);
			jbObject.put("submaxHourTime", submaxHourTime);
			jbObject.put("submaxDayTime", submaxDayTime);
			jbObject.put("subminutesTime", subminutesTime);
			jbObject.put("hourTime", hourTime);
			jbObject.put("dayTime", dayTime);
			jbObject.put("minutesTime", minutesTime);
			jbObject.put("dateString", dateString);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return jbObject.toString();
    }
}
